package cn.wsalix.order.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.BasicServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.observable.service.BuyService;
import cn.wsalix.observable.service.PayService;
import cn.wsalix.order.entity.Order;
import cn.wsalix.order.entity.OrderInfo;
import cn.wsalix.order.form.OrderForm;
import cn.wsalix.order.form.OrderInfoForm;
import cn.wsalix.order.form.WeiXinNotifyForm;
import cn.wsalix.order.repository.OrderInfoRepository;
import cn.wsalix.order.repository.PayNotifyRepository;
import cn.wsalix.order.service.OrderInfoService;
import cn.wsalix.order.status.OrderEnum;
import cn.wsalix.shop.service.PriceService;
import cn.wsalix.shop.service.ProductService;
import cn.wsalix.shop.status.PayEnum;
import cn.wsalix.type.PayTypeEnum;
import cn.wsalix.user.entity.UserInfo;
import cn.wsalix.user.service.UserInfoService;

@Service("orderInfoService")
public class OrderInfoServiceImpl extends
		BasicServiceImpl<OrderInfoForm, OrderInfoRepository, OrderInfo>
		implements OrderInfoService {
	@Autowired
	private OrderInfoRepository orderInfoRepository;
	@Autowired
	private PayNotifyRepository payNotifyRepository;
	@Autowired
	private ProductService productService;
	@Autowired
	private PriceService productPriceService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserInfoService userInfoService;

	@Override
	public String module() {
		return "/user/order_info";
	}

	@Override
	public OrderInfoRepository getRepository() {
		return orderInfoRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(OrderInfoForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getId() != null && form.getId() != 0) {
			filters.put("id", new SearchFilter("id", Operator.EQ, form.getId()));
		}
		if (form.getOrderId() != null && form.getOrderId() != 0) {
			filters.put("order.id", new SearchFilter("order.id", Operator.EQ,
					form.getOrderId()));
		}
		return filters;
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof BuyService) {
			BuyService buyService = (BuyService) o;
			OrderForm orderForm = (OrderForm) arg;
			Order order = buyService.getOrder();

			OrderInfo orderInfo = addInfo(orderForm, order);
			order.setOrderInfo(orderInfo);

		}
		if (o instanceof PayService) {
			PayService payService = (PayService) o;
			if (arg instanceof WeiXinNotifyForm) {
				WeiXinNotifyForm form = (WeiXinNotifyForm) arg;
				Order order = payService.getOrder();
				if (payService.isTreate()) {
					payed(order);
				}
			}
		}

	}

	@Transactional
	private OrderInfo addInfo(OrderForm form, Order obj) {
		SysUser user = userService.findById(form.getUserId());
		UserInfo userInfo = userInfoService.findByUser(user);
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setOrder(obj);
		orderInfo.setAvatar(userInfo.getAvatar());// 参团人数1
		orderInfo.setTradeNo(obj.getTradeNo());
		orderInfo.setOrderDate(obj.getOrderDate());
		orderInfo.setStatus(obj.getStatus());
		orderInfo.setAmount(obj.getAmount());
		orderInfo.setPostfree(obj.getPostfree());
		orderInfo.setProduct(obj.getProduct());
		orderInfo.setNum(obj.getNum());
		orderInfo.setPay(PayEnum.unPay);
		orderInfo.setPayTitle(PayEnum.unPay.getTitle());
		orderInfo.setCustomerName(userInfo.getCustomerName());
		orderInfo.setPhone(userInfo.getPhone());
		orderInfo.setAddress(userInfo.getAddress());
		orderInfo = super.add(orderInfo);
		obj.setOrderInfo(orderInfo);
		return orderInfo;
	}

	@Override
	public Class<OrderInfo> getEntityClazz() {
		return OrderInfo.class;
	}

	@Transactional
	@Override
	public void payed(Order order) {
		OrderInfo orderInfo = order.getOrderInfo();
		orderInfo.setPay(PayEnum.payed);
		orderInfo.setPayTitle(PayEnum.payed.getTitle());
		orderInfo.setStatus(OrderEnum.pending);
		orderInfo.setPayType(PayTypeEnum.weixin);
		orderInfo.setPayTypeTitle(PayTypeEnum.weixin.getTitle());
		super.edit(orderInfo);
	}

}
